﻿using System;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class PowerGroupController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public PowerGroupController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void PowerGroupList()
        {
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            var condition = String.Empty;
            DataPage<PowerGroup> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            var delclass = ctx.GetInt("delclass");
            switch (delclass)
            {
                case 1:
                    condition += " and delclass = 1 ";
                    break;

                case 2:
                    condition += " and delclass = 0 ";
                    break;
            }
            if (!condition.StrIsNullOrEmpty() &&
            condition.StartsWith(" and "))
            {
                condition = StringHelper.Right(condition, condition.Length - 5);
            }
            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "id";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "asc";
            condition += " order by " + limitkey + " " + limitclass;
            CurrentRequest.setCurrentPage(page_id);
            list = PowerGroup.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
            User user = ctx.viewer.obj as User;
            bind("memberinfo", user);
        }

        [HttpGet]
        public virtual void PowerGroupAdd()
        {
            SetPublic();
            setpublic();
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            target(PowerGroupAddDo);
            set("tab", ctx.Get("tab"));
            var list = Menulink.find(" topmlid=:t order by topmlid asc,pid asc,id asc").set("t", 0).list();
            bind("list", list);
        }

        [HttpPost]
        public virtual void PowerGroupAddDo()
        {
            PowerGroup p = new PowerGroup();
            p.powername = ctx.Post("powername");
            var powerlist = ctx.Post("powerlist[]");
            p.powerlist = powerlist.GetReplace(",", "|");
            p.delclass = 0;
            Result result = p.insert();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("grouplist_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("grouplist_add_log") + lang("log_extra_ok"), "{Id:" + p.Id + ", Name:'" + p.powername + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void PowerGroupEdit(Int64 id)
        {
            SetPublic();
            setpublic();
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            target(PowerGroupEditDo, id);
            PowerGroup p = PowerGroup.findById(id);
            bind("groupinfo", p);
            var list = Menulink.find(" topmlid=:t order by topmlid asc,pid asc,id asc").set("t", 0).list();
            bind("list", list);
        }

        [HttpPost]
        public virtual void PowerGroupEditDo(Int64 id)
        {
            PowerGroup p = PowerGroup.findById(id);
            p.powername = ctx.Post("powername");
            var powerlist = ctx.Post("powerlist[]");
            p.powerlist = powerlist.GetReplace(",", "|");
            Result result = p.update();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("grouplist_edit_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("grouplist_edit_log") + lang("log_extra_ok"), "{Id:" + p.Id + ", Name:'" + p.powername + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void PowerGroupDel()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            if (db.deleteBatch<PowerGroup>(condition) > 0)
            {
                logService.Add((User)ctx.viewer.obj, lang("grouplist_del_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("grouplist_del_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

    }
}
